<%@page language="C#" validateRequest="false" Debug="true" %>
<%@import namespace="System" %>
<%@import namespace="System.Collections.Generic" %>
<%@import namespace="System.Diagnostics" %>
<%@import namespace="System.Data"%>

<script runat="server">
  string aid = "";
  protected void Page_Load( object sender,EventArgs e )
  {
      string uid = "";
      HttpCookie ck = Request.Cookies["uid"];
      if( null==ck )
          Response.Redirect( "login.aspx" );
          
      if( string.IsNullOrEmpty(ck.Value) )
          Response.Redirect( "login.aspx" );
          
      if( null==Session[ck.Value] )
          Response.Redirect( "login.aspx" );
  }

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title> Console </title>
  <meta http-equiv="Content-Type" Content="text/html;charset=utf-8" />
  <script src="js/jquery.pack.js" type="text/javascript"></script>
  <script src="js/myjslib.js" type="text/javascript"></script>

  <style>
  .hide{ display:none; }

  #header{ position:relative; min-height:60px; background:#eeeeaa;}
  #footer{ position:relative; min-height:60px; background:#eeeecc; }
  #main{ position:relative; min-height:100px; background:#eeeebb; }
  #menu{ position:relative; min-height:30px; }
  .box{ position:relative; min-height:100px; border:1px solid #999 }
  </style>
</head>

<body>


<div id="wrap">
  <div id="header"></div>
  <div id="main"></div>
  <div id="footer"></div>
</div>

<div class="hide">
  <div id="menu">
      <a name="blog" href="#blog">博客管理</a>
      <a name="user" href="#user">用户权限管理</a>
      <a name="user" href="#user">个人信息</a>
      <a href="login.aspx?op=Logout">退出</a>
  </div>
  <div id="container"></div>

  <div id="blog_cat_box" class="box">
      <a name="blog_cat_create" href="#AddArticle">添加</a>
      <a name="blog_cat_move" href="#MoveArticles">移动</a>
      <a name="blog_cat_remove" href="#RemoveArticles">删除</a>
  </div>
  <div id="blog_art_box" class="box">
      <a name="blog_art_create" href="#AddArticle">添加</a>
      <a name="blog_art_move" href="#MoveArticles">移动</a>
      <a name="blog_art_remove" href="#RemoveArticles">删除</a>
      <button name="no">关闭</button>
  </div>
  <div id="blog_cmt_box" class="box">
      <a name="blog_cmt_create" href="#AddComment">添加</a>
      <a name="blog_cmt_remove" href="#RemoveComments">删除</a>
      <button name="no">关闭</button>
  </div>
  <div id="account_box" class="box">
      <a name="account_create" href="#AddAccount">添加</a>
      <!--<a name="account_move" href="#MoveAccounts">移动</a>-->
      <a name="account_remove" href="#RemoveAccounts">删除</a>
  </div>
</div>

<div class="hide">
<!--
标准组件
<input name='Id' type='hidden' value='0'></input>
<input name='Email' type='text' value=''></input>
<input name='Password' type='password' value=''></input>
<input name='Nickname' type='text' value=''></input>
-->
    <div id="account_list">
        <h2>帐号列表</h2>
    </div>

    <div id="account_detail">
        <button name="no">关闭</button>
        <h2>帐号详细</h2>
    </div>

    <div class="page" id="account_create">
        <h2>添加帐号</h2>
        <div>
        <form name="frm" method="post" action="#">
             Email:<input name='Email' type='text' value='' />
             Password:<input name='Password' type='password' value='' />
             Nickname:<input name='Nickname' type='text' value='' />
             <br />     <button name="ok">确定</button>
                        <button name="no">关闭</button>
        </form>
        </div>
    </div>

    <div class="page" id="account_modify">
        <h2>修改帐号</h2>
        <div>
        <form name="frm" method="post" action="#">
                   <input name='Id' type='hidden' value='0' />            
             Email:<input name='Email' type='text' value='' />
             Password:<input name='Password' type='password' value='' />
             Nickname:<input name='Nickname' type='text' value='' />
             <br />      <button name="ok">确定</button>
                         <button name="no">关闭</button>
        </form>
        </div>
    </div>

    <div class="page" id="account_remove">
        <form class="frm" method="post" action="app_load.aspx?view=blog&op=RemoveAccounts">
            <h3>确定删除选中的帐号？</h3>
            <input name="Ids" type="hidden" value="" />
            <button name="ok">确定</button>
            <button name="no">关闭</button>
        </form>
    </div>

    <div id="account_change">
        <h2>修改密码</h2>
        <form class="frm" action="app_load.aspx?view=blog&op=ChangePassword" method="post">
                         <input type="hidden" name="Id" />
            旧密码：     <input type="password" name="Password0" />
            新密码：     <input type="password" name="Password1" />
            确认新密码： <input type="password" name="Password2" />
            <button name="ok">确定</button>
            <button name="no">取消</button>
        </form>
    </div>
</div>

<div class="hide">
<!--
标准组件
<input name='Id' type='hidden' value='0'></input>
<input name='Title' type='text' value=''></input>
<input name='Description' type='text' value=''></input>
<input name='Icon' type='file' value=''></input>
<select name='ParentId'></select>
<input name='Width' type='text' value='1'></input>
-->
    <div class="page" id="blog_cat_list">
        <h2>文档分类列表</h2>
    </div>

    <div class="page" id="blog_cat_detail">
        <button name="no">关闭</button>
        <h2>文档分类详细</h2>
    </div>

    <div class="page" id="blog_cat_create">
        <h2>添加文档分类</h2>
        <div>
            <form class="frm" method="post" action="app_load.aspx?view=blog&op=AddCategory">
                <br />父分类:
                        <select name="ParentId" class="selectOptions"></select>
                <br />名称:   <input name="Title" type="text" />
                <br />描述:   <input name="Description" type="text" />
                <!--
                <br />图标:   <input name="icon" type="text" /> <a href="#">上传</a> <a href="#">浏览服务器</a>
                -->
                <br />      <button name="ok">确定</button>
                            <button name="no">关闭</button>
            </form>
        </div>
    </div>

    <div class="page" id="blog_cat_modify">
        <h2>修改文档分类</h2>
        <div>
            <form class="frm" method="post" action="app_load.aspx?view=blog&op=ModifyCategory">
                <!-- <br />父分类: <select name="parentId" class="selectOptions"></select> -->
                              <input name="Id" type="hidden" />
                <br />名称:   <input name="Title" type="text" />
                <br />描述:   <input name="Description" type="text" />
                <!--
                <br />图标:   <input name="icon" type="text" value="$icon" /> <a href="#">上传</a> <a href="#">浏览服务器</a>
                -->
                <br />      <button name="ok">确定</button>
                            <button name="no">关闭</button>
            </form>
        </div>

    </div>

    <div class="page" id="blog_cat_move">
        <h2>移动文档分类</h2>

        <form class="frm" method="post" action="app_load.aspx?view=blog&op=MoveCategories">
                <!--<input name="Title" type="text" value="" readonly="readonly" />-->
                把选中的分类
                  移动到: <select name="ParentId" class="selectOptions"></select>
                  的位置: <input name="Width" type="text" value="1" />
                          <input name="Id" type="hidden" value="" />
            <br />      <button name="ok">确定</button>
                        <button name="no">关闭</button>
        </form>
    </div>

    <div class="page" id="blog_cat_remove">
        <form class="frm" method="post" action="app_load.aspx?view=blog&op=RemoveCategories">
            <h3>确定删除选中的分类？</h3>
            <input name="Id" type="hidden" value="" />
            <button name="ok">确定</button>
            <button name="no">关闭</button>
        </form>
    </div>
</div>

<div class="hide">
<!--
标准组件
<input name='Id' type='hidden' value='0'></input>
<select name='CategoryId'></select>
<input name='Title' type='text' value=''></input>
<textarea name='Content'></textarea>
<input name='Summary' type='text' value=''></input>
<input name='Author' type='text' value=''></input>
-->
    <div class="page" id="blog_art_list">
        <h2>文章列表</h2>
    </div>

    <div class="page" id="blog_art_detail">
        <button name="no">关闭</button>
        <h2>文章详细</h2>
    </div>

    <div class="page" id="blog_art_create">
        <h2>添加文章</h2>

        <form class="frm" method="post" action="app_load.aspx?view=blog&op=AddArticle">
           分类: <input name="CategoryId" type="hidden" value="0" />
                 <input name="CategoryTitle" type="text" value="0" readonly="readonly" />
           <!--<select name="CategoryId" class="selectOptions"></select>-->
           <br />标题: <input name="Title" type="text" />
           <br />摘要: <textarea name="Summary" rows="4" cols="70"></textarea>
           <br />内容: <textarea name="Content" rows="16" cols="70" class="artTextarea"></textarea>
           <br />作者: <input name="Author" type="text" />
               <br />      <button name="ok">保存</button>
                           <button name="no">关闭</button>
           <p><strong>操作提示:</strong>  编辑比较多的内容时，应该先输入标题，然后提交，然后在修改中进行详细的编辑！</p>
        </form>
    </div>

    <div class="page" id="blog_art_modify">
        <h2>修改文章</h2>

         <form class="frm" method="post" action="app_load.aspx?view=blog&op=ModifyArticle">
            <!--分类: <select name="CategoryId" class="selectOptions"></select>-->
                        <input name="Id" type="hidden" value="" />
            <br />标题: <input name="Title" type="text" value="" />
            <br />摘要: <textarea name="Summary" rows="4" cols="70"></textarea>
            <br />内容: <textarea name="Content" rows="16" cols="70" class="artTextarea"></textarea>
            <br />作者: <input name="Author" type="text" value="" />
            <br />      <button name="ok">保存</button>
                        <button name="no">关闭</button>
            <p><strong>操作提示:</strong>  编辑比较多的内容时，应该常按保存按钮，以免因意外造成大量的内容丢失！</p>
        </form>
    </div>

    <div class="page" id="blog_art_move">
        <h2>移动文章</h2>
        <form class="frm" method="post" action="app_load.aspx?view=blog&op=MoveArticles">
            把选中的文章移动到分类:
            <input name="Ids" type="hidden" />
            <select name="NewCategoryId" class="selectOptions"></select>
            <button name="ok">确定</button>
            <button name="no">取消</button>
        </form>
    </div>

    <div class="page" id="blog_art_remove">
        <form class="frm" method="post" action="app_load.aspx?view=blog&op=RemoveArticles">
            <h3>确定删除选中的文章？</h3>
            <input name="Ids" type="hidden" />
            <button name="ok">确定</button>
            <button name="no">取消</button>
        </form>
    </div>
</div>

<div class="hide">
    <div class="page" id="blog_cmt_list">
        <h2>评论列表</h2>
    </div>

    <div class="page" id="blog_cmt_detail">
        <button name="no">关闭</button>
        <h2>评论详细</h2>
    </div>

    <div class="page" id="blog_cmt_create">
        <h2>添加评论</h2>

         <form class="frm" method="post" action="app_load.aspx?view=blog&op=AddComment">
            文章: <input name="CategoryId" type="hidden" value="0" />
                  <input name="CategoryTitle" type="text" value="0" readonly="readonly" />
            <!--<select name="CategoryId" class="selectOptions"></select>-->
            <!--
            <br />标题: <input name="Title" type="text" />
            <br />摘要: <textarea name="Summary" rows="4" cols="70"></textarea>
            -->
            <br />内容: <textarea name="Content" rows="16" cols="70" class="artTextarea"></textarea>
            <br />作者: <input name="Author" type="text" />
                <br />      <button name="ok">保存</button>
                            <button name="no">关闭</button>
            <p><strong>操作提示:</strong>  编辑比较多的内容时，应该先输入标题，然后提交，然后在修改中进行详细的编辑！</p>
        </form>
    </div>

    <div class="page" id="blog_cmt_modify">

            <h2>修改评论</h2>

             <form class="frm" method="post" action="app_load.aspx?view=blog&op=ModifyComment">
                <!--分类: <select name="CategoryId" class="selectOptions"></select>-->
                            <input name="Id" type="hidden" value="" />
                <!--<br />标题: <input name="Title" type="text" value="" />-->
                <!--<br />摘要: <textarea name="Summary" rows="4" cols="70"></textarea>-->
                <br />内容: <textarea name="Content" rows="16" cols="70" class="artTextarea"></textarea>
                <br />作者: <input name="Author" type="text" value="" />
                <br />      <button name="ok">保存</button>
                            <button name="no">关闭</button>
                <p><strong>操作提示:</strong>  编辑比较多的内容时，应该常按保存按钮，以免因意外造成大量的内容丢失！</p>
            </form>
    </div>

    <div class="page" id="blog_cmt_remove">
        <form class="frm" method="post" action="app_load.aspx?view=blog&op=RemoveComments">
            <h3>确定删除选中的评论？</h3>
            <input name="Ids" type="hidden" />
            <button name="ok">确定</button>
            <button name="no">取消</button>
        </form>
    </div>
</div>

<script type="text/javascript">

(function($){

    var uiHook = function( parentUi ){
        parentUi.nextAll().remove();
    }

    // 功能模块
    $.fn.extend(
    {
        CategoryModule:function( handler,parentUi,file,svr,callback,hook )
        {
            var hdl = handler;
            var cb  = callback;
            var ui = new Array();

            var box = [hdl,'[@name="'+hdl+'"]','#'+hdl+'_cat_box'];
            var list   = [hdl+'_cat_list',  '[@name="'+hdl+'_cat_list"]',  '#'+hdl+'_cat_list'  ];
            var detail = [hdl+'_cat_detail','[@name="'+hdl+'_cat_detail"]','#'+hdl+'_cat_detail'];
            var create = [hdl+'_cat_create','[@name="'+hdl+'_cat_create"]','#'+hdl+'_cat_create'];
            var modify = [hdl+'_cat_modify','[@name="'+hdl+'_cat_modify"]','#'+hdl+'_cat_modify'];
            var remove = [hdl+'_cat_remove','[@name="'+hdl+'_cat_remove"]','#'+hdl+'_cat_remove'];
            var move   = [hdl+'_cat_move',  '[@name="'+hdl+'_cat_move"]',  '#'+hdl+'_cat_move'  ];
            var child  = ['','',''];
            if( cb ){
                child  = [cb.hdl+'_list',  '[@name="'+cb.hdl+'_list"]',  '#'+cb.hdl+'_list'  ];
                child['item'] = '<a name="'+child[0]+'" href="#{0}">{1}</a> ';
            }else{
                child['item'] = '<span>{1}</span> ';
            }

            list['url'] =   file+'&op='+svr.list+'&id=<%=aid%>';
            detail['url'] = file+'&op='+svr.detail+'&id={0}';
            create['url'] = file+'&op='+svr.create;
            modify['url'] = file+'&op='+svr.modify;
            remove['url'] = file+'&op='+svr.remove;
            move  ['url'] = file+'&op='+svr.move;

            list['item'] = '<input name="Id" type="radio" value="{0}" />'
                           +child['item']+
                           '<a name="'+detail[0]+'" href="#{0}">详细</a>\
                           <a name="'+modify[0]+'" href="#{0}">修改</a> ';
            detail['item'] = '<div>\
                                  <div><strong>Id</strong>:{0}\
                                  <div><strong>DateCreated</strong>:{1}\
                                  <div><strong>DateModified</strong>:{2}\
                                  <div><strong>UserCreated</strong>:{3}\
                                  <div><strong>UserModified</strong>:{4}\
                                  <div><strong>Title</strong>:{5}\
                                  <div><strong>Description</strong>:{6}\
                                  <div><strong>Icon</strong>:{7}\
                                  <div><strong>ParentId</strong>:{8}\
                                  <div><strong>Path</strong>:{9}\
                                  <div><strong>Depth</strong>:{10}\
                                  <div><strong>Width</strong>:{11}\
                              </div>';

            if( hook ){ hook( parentUi );  }
            if( ui['box'] ){ ui['box'].remove();  }
            ui['box'] = $(box[2]).clone().insertAfter( parentUi );
            
            // 
            $.fn.extend(
            { 
                getCategorySelectOptions : function( currentId )
                {
                    var t = $(this);
                    $.getJSON( list['url'],function(json)
                    {
                        var decoration = "";
                        var selected = "";
                        for( var i=0,len=json.length;i<len;i++ ){
                            decoration = '-'.string(json[i].Depth-1);
                            if( currentId==json[i].Id )
                                selected = ' selected="selected"';
                            t.append( '<option value="{0}"{2}>{3}{1}</option>'.format( json[i].Id,json[i].Title,selected,decoration ) );
                            selected = "";
                        }
                    });
                }
            });
            
            /// Add
            ui['box'].find( create[1] ).click( function(){
                if( ui['create'] ){ ui['create'].remove();  }
                ui['create'] = $( create[2] ).clone().insertAfter( $(this) );

                ui['create'].find('button[@name="no"]').click(function(){ ui['create'].remove();return false;  });

                var selectElement = ui['create'].find('[@name="ParentId"]'); ///.append( '<option value="0">未分类</option>' );
                //getSelectOptions( selectElement );
                selectElement.getCategorySelectOptions();

                ui['create'].find(".frm").ajaxSubmit('json',function(json){     //绑定提交事件
                            alert( json.msg || json.error );
                });
            });

            /// List
            $.getJSON( list['url'],function(json){
                if( ui['list'] ){ ui['list'].remove();  }
                ui['list'] = $(list[2]).clone().appendTo( ui['box'] );

                // 格式化数据
                var ss = new Array();
                var endTag = new Array(); //html标签结束栈

                var curDepth = 0;

                ss.push('<form class="frm" method="post" action="#">');
                ss.push('<ul>');
                ss.push('<li><span>'.concat(json[0].Title,'</span>'));  //根节点
                for( var i=1,len=parseInt(json.length); i<len; i++ )
                {
                    var depth = parseInt(json[i].Depth);
                    //第一层包装
                    if( curDepth<depth ){
                        ss.push('<ul>');
                        endTag.push('</ul>');
                        curDepth = depth;
                    }
                    //第二层包装
                    if( curDepth==depth ){
                        ss.push('<li>');
                        endTag.push('</li>');
                    }

                    // 内容
                    ss.push( list['item'].format(json[i].Id,json[i].Title) );

                    //结束标签
                    var nextDepth;
                    if( (i+1)<len ){
                        nextDepth = parseInt(json[i+1].Depth);
                    }else{   //i+1==len
                        nextDepth = 0;
                    }
                    for( var j=0,count=curDepth-nextDepth+1;j<count;j++ )
                    {
                        if( nextDepth==curDepth ){
                            ss.push(endTag.pop());
                        }else{
                            ss.push(endTag.pop());
                            ss.push(endTag.pop());
                            curDepth--;
                        }
                    }
                }
                ss.push('</li></ul>');
                ss.push('</form>');

                $( ss.join("\n") ).appendTo( ui['list'] );

                /// Detail
                ui['list'].find( detail[1] ).click( function(){
                    if( ui['detail'] ){ ui['detail'].remove();  }
                    ui['detail'] = $( detail[2] ).clone().insertAfter( $(this) );

                    ui['detail'].find('[@name="no"]').click(function(){ ui['detail'].remove();return false;  });

                    var id = this.hash.replace("#","");
                    $.getJSON( detail['url'].format(id), function(json){
                        var s = detail['item'].format( json.Id,
                                                       json.DateCreated,
                                                       json.DateModified,
                                                       json.UserCreated,
                                                       json.UserModified,
                                                       json.Title,
                                                       json.Description,
                                                       json.Icon,
                                                       json.ParentId,
                                                       json.Path,
                                                       json.Depth,
                                                       json.Width
                                                     );
                        $(s).appendTo( ui['detail'] );
                    });
                });

                /// Modify
                ui['list'].find( modify[1] ).click( function(){
                    if( ui['modify'] ){ ui['modify'].remove();  }
                    ui["modify"] = $( modify[2] ).clone().insertAfter( $(this) );
                    ui["modify"].find('[@name="no"]').click(function(){ ui["modify"].remove();return false; });

                    var frm = ui["modify"].find(".frm");
                    var id = this.hash.replace("#","");
                    $.getJSON( detail['url'].format(id), function(json){
                        frm.find('input[@name="Id"]').val( json.Id );
                        frm.find('input[@name="Title"]').val( json.Title );
                        frm.find('input[@name="Description"]').val( json.Description );

                        frm.ajaxSubmit('json',function(json){     //绑定提交事件
                            alert( json.msg || json.error );
                        });
                    });
                });

                /// Move
                ui['box'].find( move[1] ).click( function(){
                    if( ui['move'] ){ ui['move'].remove();  }
                    ui["move"] = $( move[2] ).clone().insertAfter( $(this) );
                    ui["move"].find('[name="no"]').click(function(){ ui["move"].remove();return false; });

                    var frm = ui["move"].find(".frm");

                    var selectElement = ui['move'].find('[@name="ParentId"]'); ///.append( '<option value="0">未分类</option>' );
                    selectElement.getCategorySelectOptions();

                    frm.submit( function(){
                        $(this).find('[@name="Id"]').val(  ui['list'].getCheckedVal() );
                        return false;
                    });
                    frm.ajaxSubmit('json',function(json){     //绑定提交事件
                        alert( json.msg || json.error );
                    });
                });

                /// Remove
                ui['box'].find( remove[1] ).click( function(){
                    if( ui['remove'] ){ ui['remove'].remove();  }
                    ui["remove"] = $( remove[2] ).clone().insertAfter( $(this) );

                    ui["remove"].find('[name="no"]').click(function(){ ui["remove"].remove();return false; });

                    var frm = ui["remove"].find(".frm");

                    frm.submit( function(){
                        $(this).find('[@name="Id"]').val( ui['list'].getCheckedVal() );
                        return false;
                    });
                    frm.ajaxSubmit('json',function(json){     //绑定提交事件
                          alert( json.msg || json.error );
                    });
                });

                /// Callback
                if( cb ){
                    eval( "ui['list'].find( child[1] ).{0}( '{1}',ui['list'],'{2}',cb.svr,cb.callback );".format( cb.fn,cb.hdl,cb.file ) );
                }
            });
            return $(this);
        },
        
        // 构建向服务器取数据的Url
        // 构建Html用户界面
        ArticleModule:function( handler,parentUi,file,svr,callback,hook )
        {
            var hdl = handler;
            var cb = callback;
            var ui = new Array();

            var box = [hdl,'[@name="'+hdl+'"]','#'+hdl+'_box'];
            var list   = [hdl+'_list',  '[@name="'+hdl+'_list"]',  '#'+hdl+'_list'  ];
            var detail = [hdl+'_detail','[@name="'+hdl+'_detail"]','#'+hdl+'_detail'];
            var create = [hdl+'_create','[@name="'+hdl+'_create"]','#'+hdl+'_create'];
            var modify = [hdl+'_modify','[@name="'+hdl+'_modify"]','#'+hdl+'_modify'];
            var remove = [hdl+'_remove','[@name="'+hdl+'_remove"]','#'+hdl+'_remove'];
            var move   = [hdl+'_move',  '[@name="'+hdl+'_move"]',  '#'+hdl+'_move'  ];
            var child  = ["","",""];
            if( cb ){
                child  = [cb.hdl+'_list',  '[@name="'+cb.hdl+'_list"]',  '#'+cb.hdl+'_list'  ];
                child['item'] = '<a name="'+child[0]+'" href="#{0}">{1}</a> ';
            }else{
                child['item'] = '<span>{1}</span> ';
            }

            list['url']   = svr.list;   //file+'&op='+svr.list+'&CategoryId={0}&PageSize=100&Options=OrderDESC';
            detail['url'] = svr.detail; //file+'&op='+svr.detail+'&ArticleId={0}';
            create['url'] = svr.create; //file+'&op='+svr.create;
            modify['url'] = svr.modify; //file+'&op='+svr.modify;
            remove['url'] = svr.remove; //file+'&op='+svr.remove;
            move  ['url'] = svr.move;   //file+'&op='+svr.move;

            list['item'] = '<input name="Id" type="checkbox" value="{0}" />'
                           +child['item']+
                           '<a name="'+detail[0]+'" href="#{0}">详细</a>\
                            <a name="'+modify[0]+'" href="#{0}">修改</a> ';
            detail['item'] = '<div>\
                                 <div><strong>Id</strong>:{0}</div>\
                                 <div><strong>DateCreated</strong>:{1}</div>\
                                 <div><strong>DateModified</strong>:{2}</div>\
                                 <div><strong>UserCreated</strong>:{3}</div>\
                                 <div><strong>UserModified</strong>:{4}</div>\
                                 <div><strong>CategoryId</strong>:{5}</div>\
                                 <div><strong>Title</strong>:{6}</div>\
                                 <div><strong>Content</strong>:{7}</div>\
                                 <div><strong>Summary</strong>:{8}</div>\
                                 <div><strong>Author</strong>:{9}</div>\
                                 <div><strong>VisitedCount</strong>:{10}</div>\
                                 <div><strong>CommentsCount</strong>:{11}</div>\
                                 <div><strong>ComeFromSoftware</strong>:{12}</div>\
                             </div>';

            $(this).click( function(){
                if( hook ){ hook( parentUi );  }
                if( ui['box'] ){ ui['box'].remove();  }
                ui['box'] = $( box[2] ).clone().insertAfter( parentUi );
                ui['box'].find('[name="no"]').click(function(){ ui['box'].remove();return false; });

                var id = this.hash.replace("#","");

                /// Add
                var CategoryTitle = $(this).html();
                ui['box'].find( create[1] ).click(function(){
                    if( ui['create'] ){ ui['create'].remove();  }
                    ui['create'] = $( create[2] ).clone().insertAfter( $(this) );

                    ui['create'].find('[name="no"]').click(function(){ ui['create'].remove();return false; });

                    var frm = ui['create'].find(".frm");

                    frm.find( 'input[@name="CategoryId"]' ).val( id );
                    frm.find( 'input[@name="CategoryTitle"]' ).val( CategoryTitle );

                    frm.ajaxSubmit('json',function(json){     //绑定提交事件
                                alert( json.msg || json.error );
                    });
                });

                /// List
                $.getJSON( list['url'].format(id,1), function(json){
                    if( ui['list'] ){ ui['list'].remove();  }
                    ui['list'] = $(list[2]).clone().appendTo( ui['box'] );

                    var ls = json.Content;
                    var pg = json.Page;

                    var ss = new Array();
                    ss.push( "<ul>" );
                    for( var i=0,len=ls.length;i<len;i++ ){
                        ss.push( '<li>'+list['item'].format( ls[i].Id,ls[i].Title )+'</li>' );
                    }
                    ss.push( "</ul>" );
                    $( ss.join("\n") ).appendTo( ui['list'] );

                    /// Detail
                    ui['list'].find( detail[1] ).click(function(){
                        if( ui['detail'] ){ ui['detail'].remove();  }
                        ui['detail'] = $( detail[2] ).clone().insertAfter( $(this) );
                        ui['detail'].find('[name="no"]').click(function(){ ui['detail'].remove();return false; });

                        var id = this.hash.replace("#","");
                        $.getJSON( detail['url'].format(id), function(json){
                            var s = detail['item'].format( json.Id,
                                                           json.DateCreated,
                                                           json.DateModified,
                                                           json.UserCreated,
                                                           json.UserModified,
                                                           json.CategoryId,
                                                           json.Title,
                                                           json.Content,
                                                           json.Summary,
                                                           json.Author,
                                                           json.VisitedCount,
                                                           json.CommentsCount,
                                                           json.ComeFromSoftware
                                                          );
                            $(s).appendTo( ui['detail'] );
                        });
                    });

                    /// Modify
                    ui['list'].find( modify[1] ).click(function(){
                        if( ui["modify"] ){ ui["modify"].remove();  }
                        ui["modify"] = $( modify[2] ).clone().insertAfter( $(this) );
                        ui["modify"].find('[name="no"]').click(function(){ ui["modify"].remove();return false; });

                        var id = this.hash.replace("#","");
                        $.getJSON( detail['url'].format(id), function(json){
                             ui["modify"].find('input[@name="Id"]').val( json.Id );
                             ui["modify"].find('input[@name="Title"]').val( json.Title );
                             ui["modify"].find('[@name="Summary"]').val( json.Summary );
                             ui["modify"].find('[@name="Content"]').val( json.Content );
                             ui["modify"].find('input[@name="Author"]').val( json.Author );
                        });
                        ui["modify"].find(".frm").ajaxSubmit('json',function(json){     //绑定提交事件
                              alert( json.msg || json.error );
                        });
                    });

                    /// Move
                    ui['box'].find( move[1] ).click(function(){
                        if( ui["move"] ){ ui["move"].remove();  }
                        ui["move"] = $( move[2] ).clone().insertAfter( $(this) );
                        ui["move"].find('[name="no"]').click(function(){ ui["move"].remove();return false; });

                        var frm = ui["move"].find(".frm");

                        var selectElement = ui['move'].find('[@name="NewCategoryId"]'); ///.append( '<option value="0">未分类</option>' );
                        selectElement.getCategorySelectOptions();
                        
                        frm.submit( function(){
                            $(this).find('[@name="Ids"]').val( ui['list'].getCheckedVal() );
                            return false;
                        });
                        frm.ajaxSubmit('json',function(json){     //绑定提交事件
                            alert( json.msg || json.error );
                        });
                    });

                    /// Remove
                    ui['box'].find( remove[1] ).click(function(){
                        if( ui["remove"] ){ ui["remove"].remove();  }
                        ui["remove"] = $( remove[2] ).clone().insertAfter( $(this) );

                        ui["remove"].find('[name="no"]').click(function(){ ui["remove"].remove();return false; });

                        var frm = ui["remove"].find(".frm");

                        frm.submit( function(){
                            $(this).find('[@name="Ids"]').val( ui['list'].getCheckedVal() );
                            return false;
                        });
                        frm.ajaxSubmit('json',function(json){     //绑定提交事件
                              alert( json.msg || json.error );
                        });
                    });

                    /// Callback
                    if( cb ){
                        eval( "ui['list'].find( child[1] ).{0}( '{1}',ui['list'],'{2}',cb.svr,cb.callback );".format( cb.fn,cb.hdl,cb.file ) );
                    }

                });
            });
            return $(this);
        },

        AccountModule:function( handler,parentUi,file,svr,callback,hook )
        {
            // 读取Html
            // 发送Url
            var hdl = handler;
            var cb = callback;
            var ui = new Array();

            var box = [hdl,'[@name="'+hdl+'"]','#'+hdl+'_box'];
            var list   = [hdl+'_list',  '[@name="'+hdl+'_list"]',  '#'+hdl+'_list'  ];
            var detail = [hdl+'_detail','[@name="'+hdl+'_detail"]','#'+hdl+'_detail'];
            var create = [hdl+'_create','[@name="'+hdl+'_create"]','#'+hdl+'_create'];
            var modify = [hdl+'_modify','[@name="'+hdl+'_modify"]','#'+hdl+'_modify'];
            var remove = [hdl+'_remove','[@name="'+hdl+'_remove"]','#'+hdl+'_remove'];
            var change = [hdl+'_change','[@name="'+hdl+'_change"]','#'+hdl+'_change']; //修改密码
            var child  = ["","",""];
            if( cb ){
                child  = [cb.hdl+'_list',  '[@name="'+cb.hdl+'_list"]',  '#'+cb.hdl+'_list'  ];
                child['item'] = '<a name="'+child[0]+'" href="#{0}">{1}</a> ';
            }else{
                child['item'] = '<span>{1}</span> ';
            }

            list['url']   = svr.list;
            detail['url'] = svr.detail;
            create['url'] = svr.create;
            modify['url'] = svr.modify;
            remove['url'] = svr.remove;

            list['item'] = '<input name="Id" type="checkbox" value="{0}" />'
                           +child['item']+
                           '<a name="'+detail[0]+'" href="#{0}">详细</a>\
                            <a name="'+modify[0]+'" href="#{0}">修改</a>';
                            //<a name="'+change[0]+'" href="#{0}">修改密码</a> ';
            detail['item'] = '<div>\
                                 <div><strong>id</strong>:{0}</div>\
                                 <div><strong>date_created</strong>:{1}</div>\
                                 <div><strong>date_modified</strong>:{2}</div>\
                                 <div><strong>user_created</strong>:{3}</div>\
                                 <div><strong>user_modified</strong>:{4}</div>\
                                 <div><strong>email</strong>:{5}</div>\
                                 <div><strong>password</strong>:{6}</div>\
                                 <div><strong>nickname</strong>:{7}</div>\
                                 <div><strong>times_active</strong>:{8}</div>\
                                 <div><strong>last_active</strong>:{9}</div>\
                                 <div><strong>has_user</strong>:{10}</div>\
                             </div>';
            
            // 整理界面
            if( hook ){ hook( parentUi );  }
            if( ui['box'] ){ ui['box'].remove();  }
            ui['box'] = $( box[2] ).clone().insertAfter( parentUi );
            ui['box'].find('[name="no"]').click(function(){ ui['box'].remove();return false; }); 
             
            /// Add
            ui['box'].find( create[1] ).click(function(){
                if( ui['create'] ){ ui['create'].remove();  }
                ui['create'] = $( create[2] ).clone().insertAfter( $(this) );
            
                ui['create'].find('[name="no"]').click(function(){ ui['create'].remove();return false; });
            
                var frm = ui['create'].find("[@name='frm']");
                frm[0].action = create.url;
                frm.ajaxSubmit('json',function(json){     //绑定提交事件
                            alert( json.msg || json.error );
                });
            });
          
            /// List
            $.getJSON( list.url.format("1"),function(json){
                if( ui['list'] ){ ui['list'].remove();  }
                ui['list'] = $(list[2]).clone().appendTo( ui['box'] );

                var ls = json.Content;
                var pg = json.Page;

                var ss = new Array();
                ss.push( "<ul>" );
                for( var i=0,len=ls.length;i<len;i++ ){
                    ss.push( '<li>'+list['item'].format( ls[i].Id,ls[i].Email+'('+ls[i].Nickname+')' )+'</li>' );
                }
                ss.push( "</ul>" );
                $( ss.join("\n") ).appendTo( ui['list'] );
                
                /// Detail
                ui['list'].find( detail[1] ).click(function(){
                    if( ui['detail'] ){ ui['detail'].remove();  }
                    ui['detail'] = $( detail[2] ).clone().insertAfter( $(this) );
                    ui['detail'].find('[name="no"]').click(function(){ ui['detail'].remove();return false; });
                    
                    var id = this.hash.replace("#","");
                    $.getJSON( detail['url'].format(id), function(json){
                    var s = detail['item'].format( json.Id,
                                                   json.DateCreated,
                                                   json.DateModified,
                                                   json.UserCreated,
                                                   json.UserModified,
                                                   json.Email,
                                                   json.Password,
                                                   json.Nickname,
                                                   json.TimesActive,
                                                   json.LastActive,
                                                   json.HasUser
                                                 );
                         $(s).appendTo( ui['detail'] );
                    });
                });
                
                /// Modify
                ui['list'].find( modify[1] ).click(function(){
                    if( ui["modify"] ){ ui["modify"].remove();  }
                    ui["modify"] = $( modify[2] ).clone().insertAfter( $(this) );
                    ui["modify"].find('[name="no"]').click(function(){ ui["modify"].remove();return false; });

                    var id = this.hash.replace("#","");
                    $.getJSON( detail['url'].format(id), function(json){
                         ui["modify"].find('input[@name="Id"]').val( json.Id );
                         ui["modify"].find('input[@name="Email"]').val( json.Email );
                         ui["modify"].find('[@name="Password"]').val( json.Password );
                         ui["modify"].find('[@name="Nickname"]').val( json.Nickname );
                    });
                    var frm = ui["modify"].find("[@name='frm']");
                    frm[0].action = modify.url;
                    frm.ajaxSubmit('json',function(json){     //绑定提交事件
                          alert( json.msg || json.error );
                    });
                });
                
                /// Remove
                ui['box'].find( remove[1] ).click(function(){
                    if( ui["remove"] ){ ui["remove"].remove();  }
                    ui["remove"] = $( remove[2] ).clone().insertAfter( $(this) );
                
                    ui["remove"].find('[name="no"]').click(function(){ ui["remove"].remove();return false; });
                
                    var frm = ui["remove"].find(".frm");
                
                    frm.submit( function(){
                        $(this).find('[@name="Ids"]').val( ui['list'].getCheckedVal() );
                        return false;
                    });
                    frm.ajaxSubmit('json',function(json){     //绑定提交事件
                          alert( json.msg || json.error );
                    });
                });
            });
            return $(this);
        }
    });


    // 用户模块
    $.fn.extend(
    {
        Blog : function( ui )
        {
            // 服务端数据接口
            var catSvr = { list:'GetCategories',
                           detail:'GetCategory',
                           create:'AddCategory',
                           modify:'ModifyCategory',
                           remove:'RemoveCategories',
                           move:'MoveCategories' 
                         };
            var artSvr = { list:  '/app_load.aspx?view=blog&op=GetArticles&CategoryId={0}&PageSize=20&Page={1}&Options=OrderDESC',
                           detail:'/app_load.aspx?view=blog&op=GetArticle&ArticleId={0}',
                           create:'/app_load.aspx?view=blog&op=AddArticle',
                           modify:'/app_load.aspx?view=blog&op=ModifyArticle',
                           remove:'/app_load.aspx?view=blog&op=RemoveArticles',
                           move:  '/app_load.aspx?view=blog&op=MoveArticles' 
                         };
            var cmtSvr = { list:  '/app_load.aspx?view=blog&op=GetComments&ArticleId={0}&PageSize=20&Page={1}&Options=OrderDESC',
                           detail:'/app_load.aspx?view=blog&op=GetComment&CommentId={0}',
                           create:'/app_load.aspx?view=blog&op=AddComment',
                           modify:'/app_load.aspx?view=blog&op=ModifyComment',
                           remove:'/app_load.aspx?view=blog&op=RemoveComments',
                           move:  '/app_load.aspx?view=blog&op=MoveArticles' 
                         };

            // 有子模块的模块需要callback属性,此属性指向子模块配置对象
            var confCmt = { fn:'ArticleModule', hdl:'blog_cmt',file:'/app_load.aspx?view=blog',svr:cmtSvr };
            var confArt = { fn:'ArticleModule', hdl:'blog_art',file:'/app_load.aspx?view=blog',svr:artSvr,callback:confCmt };
            var confCat = { fn:'CategoryModule',hdl:'blog',    file:'/app_load.aspx?view=blog',svr:catSvr,callback:confArt,hook:uiHook };

            // 入口配置对象
            var entry = confCat;

            //obj = eval( 'menu.find(\'[@name="{1}"]\').{0}( "{1}",ui,"{2}",obj.svr,obj.callback,obj.hook );'.format( obj.fn,obj.hdl,obj.file ) );
            return $(this).click( function()
            {
                var obj = entry;
                $(this).CategoryModule( obj.hdl,ui,obj.file,obj.svr,obj.callback,obj.hook );
            });
        },

        Account : function( ui )
        {
            var accSvr = { list:  '/app_load.aspx?view=account&op=GetAccounts&PageSize=20&Page={0}&Options=OrderDESC',
                           detail:'/app_load.aspx?view=account&op=GetAccount&AccountId={0}',
                           create:'/app_load.aspx?view=account&op=AddAccount',
                           modify:'/app_load.aspx?view=account&op=ModifyAccount',
                           remove:'/app_load.aspx?view=account&op=RemoveAccounts',
                           exists:'/app_load.aspx?view=account&op=IsAccountExists', 
                           chPass:'/app_load.aspx?view=account&op=ChangePassword'
                         };
            var confAcc = { fn:'AccountModule',hdl:'account',file:'/app_load.aspx?view=blog',svr:accSvr,hook:uiHook };
            var entry = confAcc;

            return $(this).click(function()
            {
                var obj = entry;
                $(this).AccountModule( obj.hdl,ui,obj.file,obj.svr,obj.callback,obj.hook );
            });
        }
    });

    // 应用程序类
    $.fn.extend(
    {
        App : function()  //= window.App =
        {
            var menu = $("#menu").clone().appendTo( $(this) );

            var blog = menu.find("[name='blog']").Blog( menu );
            var user = menu.find("[name='user']").Account( menu );

            return $(this).load(function(){
                blog.click();  //默认显示
            });
        }
    });

    // Start
    $(function()
    {
        $("#main").App().load();
    });

})(jQuery);


</script>

</body>
</html>